﻿using System.Collections;
using System.Collections.Generic;
using System.Data.Objects.DataClasses;
using System.Web.Mvc;
using DNBProject.Model.PopupPartial;

namespace DNBProject.Common.PopupInfo
{
    /// <summary>
    /// The poup up template
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class BasePopupTemplate<T> where T : EntityObject
    {
        /// <summary>
        /// Gets the Ky Hieu column name of entity
        /// </summary>
        public virtual string KyHieuName
        {
            get { return "colKyhieu"; }
        }

        /// <summary>
        /// Gets the Vi Tri column name of entity
        /// </summary>
        public virtual string ViTriName
        {
            get { return "colVitri"; }
        }

        /// <summary>
        /// Gets the Mo ta column name of entity
        /// </summary>
        public virtual string MoTaName
        {
            get { return "colMota"; }
        }

        /// <summary>
        /// Gets the Toa Do column name of entity
        /// </summary>
        public virtual string ToaDoName
        {
            get { return ""; }
        }

        /// <summary>
        /// Get html code to display info
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        public virtual string GetInfo(Controller controller, T entity = null)
        {
            if (entity == null || controller == null) //Not pass entity
            {
                return "";
            }

            //Get the current type of entity
            var typeEntity = typeof(T);

            //Create model to pass into view
            var model = new InfoPopupModel()
                {
                    KyHieu = typeEntity.GetProperty(KyHieuName).GetValue(entity, null),
                    ViTri = typeEntity.GetProperty(ViTriName).GetValue(entity, null),
                    MoTa = typeEntity.GetProperty(MoTaName).GetValue(entity, null),
                    ToaDo = ""
                };
            
            //Get html result from partial view
            var result = ControllerExtensions.RenderPartialViewToString(controller, "PopupPartial/InfoPopupPartial", model);

            return result;
        }

        /// <summary>
        /// Get html code to display table
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="entity"></param>
        /// <param name="dynamicSource"></param>
        /// <returns></returns>
        public virtual string GetInfoTable(Controller controller, T entity, object dynamicSource)
        {
            var source = dynamicSource as IList<RowPopupModel>;

            if (source == null)
            {
                return "";
            }

            if (source.Count <= 0)
            {
                return "";
            }

            var model = new TablePopupModel
                {
                    Title = "10/7/2014",
                    Rows = source
                };

            //Get html result from partial view
            var result = ControllerExtensions.RenderPartialViewToString(controller, "PopupPartial/TablePopupPartial", model);

            return result;
        }

        /// <summary>
        /// Get html code to display chart
        /// </summary>
        /// <param name="controller"></param>
        /// <param name="entity"></param>
        /// <param name="dynamicSource"></param>
        /// <returns></returns>
        public virtual string GetInfoChart(Controller controller, T entity, object dynamicSource)
        {
            var source = dynamicSource as IList<RowPopupModel>;

            if (source == null)
            {
                return "";
            }

            if (source.Count <= 0)
            {
                return "";
            }

            var model = new TablePopupModel
            {
                Title = "10/7/2014",
                Rows = source
            };

            //Get html result from partial view
            var result = ControllerExtensions.RenderPartialViewToString(controller, "PopupPartial/HighChartPopupPartial", model);

            return result;
        }

        /// <summary>
        /// Get html code to display header
        /// </summary>
        /// <returns></returns>
        public virtual string GetHeader(Controller controller)
        {
            return ControllerExtensions.RenderPartialViewToString(controller, "PopupPartial/HeaderPopupPartial", null);
        }
    }
}